-
What should be or is told to me is incredibly simple - sending commands via serial or wifi has been an incredible struggle in micropython
-
I uploaded the code to esp32-c3 and building the web server was incredibly easy. Same process as the xiaorp2040
-
Adapting the music code was also relatively easy as well - i just needed to change the pins and how I called them
-
Now, just trying to communicate between the board and the web server is proving to be a huge challenge
-
I cannot find any example so far that shows me how to do this and been struggling to understand how microcontrollers internally kinda work with communication
import network
import socket
import machine
import threading
from time import sleep
ssid = 'EECS_Labs'
password = ''
#ssid = ''
#password = ''
# Define the pin connected to the LM4871 amplifier
signal_pin = machine.Pin(10, machine.Pin.OUT)
shutdown_pin = machine.Pin(9, machine.Pin.OUT)
# Create a PWM object
pwm = machine.PWM(signal_pin)
# Set initial state of the PWM signal
pwm.duty(0) # Turn off the sound
# Variable to track the state of the music
music_playing = False
def connect():
# Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def play_music():
# Add your music playing logic here
pwm.duty(300) # Set duty cycle for sound
pwm.freq(440) # Set frequency (440 Hz is an example)
def stop_music():
# Turn off the sound
pwm.duty(0)
def play_music_thread():
global music_playing
while music_playing:
play_music()
def serve(connection):
global music_playing
# Start a web server
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
print('Received request:', request)
if request == '/togglemusic':
print('Toggling music...')
if music_playing:
# Stop playing music
music_playing = False
stop_music()
else:
# Start playing music in a separate thread
music_playing = True
threading.Thread(target=play_music_thread).start()
client.send("HTTP/1.1 200 OK\nContent-Length: {}\n\n{}".format(len(html_css_code), html_css_code))
client.close()
try:
# Set initial state of the shutdown pin
shutdown_pin.off() # Assuming high is off, adjust if necessary
ip = connect()
connection = open_socket(ip)
pwm.duty(0) # Turn off the sound
serve(connection)
except KeyboardInterrupt:
# Set the shutdown pin to high before resetting
shutdown_pin.on() # Assuming high is off, adjust if necessary
machine.reset()
Final thoughts
-
I eventually got the button to start taking in commands and sending them to the microcontroller
-
There was a trailing ? when parsing web data that was getting in the way for a while
-
I ran into more issues when trying to stop becuase the threading package was not wokring as expected
-
Working with micropython and also trying to use chatgpt to debug is difficult due to the newness of micropython
-
I eventually just got rid of the lines that were giving me trouble and it somehow still worked thankfully